package com.example.ccj.b.c.custom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ccj.R;
import com.example.ccj.b.c.custom.image.MLocalMedia;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.ToastUtils;

import java.util.List;


/**
 * 图片加载
 * 2022年10月20日18:30:46
 */
public class AdpterShowImages extends RecyclerView.Adapter<AdpterShowImages.LinearViewHolder>{
    private static final String TAG = "rec图片样式（AdpterForRecyclerImagesLoadAndRemoveButton）";

    private final Context mContext;
    private final OnItemClickListener clickListener;
    private final List<MLocalMedia> imagesArray;
    private final boolean isCanDelete;

    private OnItemDeleteListener deleteListener;

    //构造方法
    public AdpterShowImages(Context context,
                            List<MLocalMedia> images, boolean isCanDelete,
                            OnItemClickListener clickListener,
                            OnItemDeleteListener deleteListener){
        this.mContext = context;
        this.imagesArray = images;
        this.isCanDelete = isCanDelete;
        this.clickListener = clickListener;
        this.deleteListener = deleteListener;
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    @NonNull
    @Override
    public LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_show_image, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final LinearViewHolder holder, @SuppressLint("RecyclerView") final int position) {

        //当前显示的显示绿色
        if (imagesArray.get(position).nowShow) {
            holder.lini.setBackgroundColor(Color.parseColor("#FF00FF0A"));
        } else if (imagesArray.get(position).selected){//选中的显示蓝色
            holder.lini.setBackgroundColor(Color.parseColor("#0027FF"));
        } else {//其他无颜色
            holder.lini.setBackgroundColor(0);
        }

        ImageUtils.loadImage(mContext,imagesArray.get(position).getAvailablePath(),holder.mImageView);



        if (isCanDelete) {
            holder.mImageDelete.setVisibility(View.VISIBLE);
            //删除图片事件
            holder.mImageDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (deleteListener != null) {
//                    MyImages myImages = imagesArray.get(position);
                        removeData(position);
                        deleteListener.onDelete(imagesArray,imagesArray.get(position),position);

                    }else{
                        ToastUtils.showShort("删除失败，未设置删除回调事件");
                    }
                }
            });
        }

        holder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onItemsClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return imagesArray.size();
    }

    static class LinearViewHolder extends  RecyclerView.ViewHolder{
        private ImageView mImageView;
        private ImageView mImageDelete;
        private LinearLayout lini;
        public LinearViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.images_and_remove_images);
            mImageDelete = itemView.findViewById(R.id.images_and_remove_remove);
            lini = itemView.findViewById(R.id.images_and_remove_ili);
        }
    }

    //  删除item
    public void removeData(int position) {
        imagesArray.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    /**
     * 设置图片尺寸
     * @param size
     */
    public void setImageSize(int size){

    }

    boolean isGoneDeleteIcon = false;
    /**
     * 隐藏删除按钮
     */
    public void goneDeleteIcon(boolean isGone){
        isGoneDeleteIcon = isGone;
    }

    /**
     * Item点击事件回调
     */
    public interface OnItemClickListener{
        void onItemsClick(int po);
    }

    /**
     * Item点击事件回调
     */
    public interface OnItemDeleteListener{
        void onDelete(List<MLocalMedia> myImages, MLocalMedia deleteImage,int position);
    }
}
